package com.groupThree.controller;

import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.groupThree.bean.*;
import com.groupThree.mvc.ResponseBody;
import com.groupThree.service.CourierService;
import com.groupThree.service.ExpressService;
import com.groupThree.util.DateFormatUtil;
import com.groupThree.util.JSONUtil;
import com.groupThree.util.UserUtil;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {
    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Integer>> data = CourierService.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String c_name = request.getParameter("c_name");
        String c_phone = request.getParameter("c_phone");
        String c_idcard = request.getParameter("c_idcard");
        String c_password = request.getParameter("c_password");

        Courier c = new Courier(c_name, c_phone, c_idcard, c_password);
        Message msg = new Message();
        StateOfAU flag = CourierService.insert(c);
        int sta = flag.getStateAu();
        switch (sta){
            case 0:  //成功
                msg.setStatus(0);
                msg.setResult("录入成功！");
                break;
            case 1:
                msg.setStatus(-1);
                msg.setResult("电话号码已经存在！");
                break;
            case 2:
                msg.setStatus(-1);
                msg.setResult("身份证号码已经存在！");
                break;
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);

        List<BootStrapTableCourier> list2 = new ArrayList<>();
        List<Map<String,Integer>> console = CourierService.console();
        for (Courier c: list){
            String regristertime = DateFormatUtil.format(c.getRegistertime());
            String logintime = c.getLogintime()==null?regristertime:DateFormatUtil.format(c.getLogintime());
            String c_deliveries = c.getC_deliveries()==0?"无派件信息":String.valueOf(c.getC_deliveries());
            int size = c.getC_password().length();
            String v_password = "*";
            String c_password = "";
            for (int i= 0;i<size;i++){
                c_password = v_password+c_password;
            }
            BootStrapTableCourier c2 = new BootStrapTableCourier(c.getC_id(),c.getC_name(),c.getC_phone(),c.getC_idcard(),c_password,c_deliveries,regristertime,logintime);
            list2.add(c2);
        }
        Integer total = console.get(0).get("data_size");

        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response) {
        String c_phone = request.getParameter("c_phone");
        Courier c = CourierService.findByC_Phone(c_phone);
        Message msg = new Message();
        if (c.getC_name()== null) {
            msg.setStatus(-1);
            msg.setResult("该手机号不存在");
        } else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(c);

        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        int c_id = Integer.parseInt(request.getParameter("c_id"));
        boolean flag = CourierService.deleteByC_ID(c_id);
        Message msg = new Message();

        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功！");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败！");
        }

        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response) {
        int c_id = Integer.parseInt(request.getParameter("c_id"));
        String c_name = request.getParameter("c_name");
        String c_phone = request.getParameter("c_phone");
        String c_idcard = request.getParameter("c_idcard");
        String c_password = request.getParameter("c_password");

        Courier newCourier = new Courier();

        newCourier.setC_name(c_name);
        newCourier.setC_phone(c_phone);
        newCourier.setC_idcard(c_idcard);
        newCourier.setC_password(c_password);
        StateOfAU flag = CourierService.update(c_id, newCourier);

        Message msg = new Message();
        int sta = flag.getStateAu();
        switch (sta){
            case 0:  //成功
                msg.setStatus(0);
                msg.setResult("修改成功！");
                break;
            case 1:
                msg.setStatus(-1);
                msg.setResult("电话号码已经存在！");
                break;
            case 2:
                msg.setStatus(-1);
                msg.setResult("身份证号码已经存在！");
                break;
            case -1:
                msg.setStatus(-1);
                msg.setResult("修改失败");
        }


        String json = JSONUtil.toJSON(msg);
        return json;


    }
}
